import org.junit.Test;

import java.io.*;
public class BufferedT {
    public static void main(String[] args) {
        
    }

    @Test
    public void BufferedStreamTest() throws FileNotFoundException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            File SP = new File("I:\\HD-cmn-215\\CVC.mp4");
            File GP = new File("I:\\HD-cmn-215\\CVC2.mp4");
            FileInputStream fis = new FileInputStream((SP));
            FileOutputStream fos = new FileOutputStream(GP);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1048576];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            //关闭外层流的同时也会自动的进行关闭内层流
        }
    }
    public void copyFileWithBuffered(String sp1, String dp1) {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            File SP = new File(sp1);
            File GP = new File(dp1);
            FileInputStream fis = new FileInputStream((SP));
            FileOutputStream fos = new FileOutputStream(GP);
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);
            byte[] buffer = new byte[1048576];
            int len;
            while ((len = bis.read(buffer)) != -1) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    @Test
    public void testCopyFileWithBuffered() {
        long start = System.currentTimeMillis();

        String sp1 = "I:\\HD-cmn-215\\CVC.mp4";
        String dp1 = "I:\\HD-cmn-215\\CVC2.mp4";

        copyFileWithBuffered(sp1, dp1);

        long end = System.currentTimeMillis();

        System.out.println("Time is ：" + (end - start));//618 - 176
    }


    @Test
    public void testBufferedReaderBufferedWriter() {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new FileReader(new File("NP666.txt")));
            bw = new BufferedWriter(new FileWriter(new File("NP996.txt")));
            String data;
            while ((data = br.readLine()) != null) {
                //方法一：
//                bw.write(data + "\n");//data中不包含换行符
                //方法二：
                bw.write(data);//data中不包含换行符
                bw.newLine();//提供换行的操作

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (bw != null) {

                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
